-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
58 lines (51 loc) · 1.14 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <stack>
using namespace std;
class Queue {
stack<int> s1, s2;
public:
void enqueue(int value) {
s1.push(value);
}
int dequeue() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
if (s2.empty()) {
cout << "Queue is empty\n";
return -1;
}
int value = s2.top();
s2.pop();
return value;
}
};
int main() {
Queue q;
int choice, value;
do {
cout << "\n1. Enqueue\n2. Dequeue\n3. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
q.enqueue(value);
break;
case 2:
value = q.dequeue();
if (value != -1)
cout << "Dequeued value: " << value << endl;
break;
case 3:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 3);
return 0;
}